package com.teamtgv.shareshark.task;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.util.Log;

import com.teamtgv.shareshark.AppData;
import com.teamtgv.shareshark.JSONParser;

/**
 * Background Async Task to Create new product
 * */
public class UpdateImageUrlTask extends AsyncTask<String, Void, Void> {

	public static final String LOG_TAG = UpdateImageUrlTask.class.getSimpleName();
	private int recordId;

	public UpdateImageUrlTask(int recordId) {
		this.recordId = recordId;
	}

	/**
	 * Before starting background thread Show Progress Dialog
	 * */
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
	}

	/**
	 * Creating product
	 * */
	protected Void doInBackground(String... args) {
		String imageUrl = args[0];

		// Building Parameters
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("id", String.valueOf(recordId)));
		params.add(new BasicNameValuePair("url", imageUrl));

		JSONParser jsonParser = new JSONParser();
		// getting JSON Object
		// Note that create product url accepts POST method
		JSONObject json = jsonParser.makeHttpRequest(AppData.FULL_HOST + "update_image.php", "POST",
				params);

		if (json == null) {
        	Log.v(LOG_TAG, "JSON object from HTTP request is null");
        	return null;
        }
		
		
		// check log cat fro response
		Log.d("Create Response", json.toString());

		// check for success tag
		try {
			int success = json.getInt("success");

			if (success == 1) {
				// successfully created product
				Log.v(LOG_TAG,
						"JSON updating image response: "
								+ json.getString("message"));
			} else {
				// failed to create product
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return null;
	}

}